package com.erp.erp_project.controller;/**
 * @author yh
 */

import cn.hutool.crypto.SecureUtil;
import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.core.base.AjaxResult;
import com.erp.erp_project.core.exception.CustomException;
import com.erp.erp_project.model.Power;
import com.erp.erp_project.model.Role;
import com.erp.erp_project.model.Submenu;
import com.erp.erp_project.model.User;
import com.erp.erp_project.service.PowerService;
import com.erp.erp_project.service.RoleService;
import com.erp.erp_project.service.SubmenuService;
import com.erp.erp_project.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.application.Application;
import javafx.stage.Stage;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/user")
public class UserController{
    private UserService userService;
    private SubmenuService submenuService;
    private PowerService powerService;
    private RoleService roleService;

    @GetMapping("/list")
    public String list(){
        return "user/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public AjaxResult<PageInfo<User>> list1(User user,Integer page,Integer limit,String userfield,String condition){
            PageHelper.startPage(page,limit);
            List<User> users=userService.list(user,userfield,condition);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return new AjaxResult<PageInfo<User>>(pageInfo);
    }

    @GetMapping("/power")
    public String power(Integer roleId, Model model){
            Role role=roleService.query(roleId);
            List<Submenu> submenus=submenuService.list(new Submenu());
            model.addAttribute("submenus",submenus);
            String strs=powerService.query(roleId);
            List<Power> powers=null;
            List<Power> powers1=null;
            if(strs!=null&&!strs.trim().equals("")){
                //角色的权限
                powers=powerService.selects(strs);
                List<String> list=new ArrayList<String>();
                for (Power power:powers
                ) {
                    list.add(power.getName());
                }

                powers1=powerService.list(new Power());
                //角色没有的权限
                Iterator<Power> it=powers1.iterator();
                while (it.hasNext()){
                    if(list.contains(it.next().getName())){
                        it.remove();
                    }
                }

            }else {
                powers1=powerService.list(new Power());
            }




        model.addAttribute("powers",powers);
        model.addAttribute("powers1",powers1);
        model.addAttribute("role",role);

        return "user/power";
    }

    @PostMapping("/power")
    @ResponseBody
    public AjaxResult power1(String ids, Role role, HttpServletRequest request) throws CustomException {
        HttpSession session=request.getSession(true);
        User user=(User) session.getAttribute("user");
        if(!user.getRole().getRoleName().equals("超级管理员")){
            throw new CustomException("不是超级管理员不能修改权限");
        }
        roleService.updatarole(role);
        ids=ids.replace("[","");
        ids=ids.replace("]","");
        ids=ids.replace("\"","");
        roleService.updatarp(ids,role.getRoleId());
        return new AjaxResult("保存成功");
    }

    @GetMapping("/add")
    @Permission("用户添加")
    public String add(Model model, Role role){
        List<Role> roles=roleService.list(role);
        model.addAttribute("roles",roles);
        return "user/add";
    }

    @PostMapping("/add")
    @ResponseBody
    @Permission("用户添加")
    public AjaxResult add1(User user,Role role){
        User user2=userService.selectuser(user);
        if(user2!=null){
            return new AjaxResult(500,"用户名已存在");
        }
        user.setRole(role);
        user.setPassword(SecureUtil.md5(user.getPassword()));
        userService.insert(user);
        User user1=userService.selectuser(user);
        user.setUserId(user1.getUserId());
        roleService.add(user);
        return new AjaxResult("添加成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    @Permission("用户删除")
    public AjaxResult delete(String userIds){
        String[] uid=userIds.split(",");
        List ids= Arrays.asList(uid);
        userService.delete(ids);
        return new AjaxResult("删除成功");
    }

    @GetMapping("/update")
    @Permission("用户修改")
    public String update(User user,Model model){
        List<Role> roles=roleService.list(new Role());
        model.addAttribute("roles",roles);
        User updateuser=userService.updatequery(user);
        model.addAttribute("updateuser",updateuser);
        return "user/update";
    }

    @PostMapping("/update")
    @ResponseBody
    @Permission("用户修改")
    public AjaxResult update1(User user,Role role){
        User user2=userService.selectuser(user);
        if(user2!=null&&!user2.getUserId().equals(user.getUserId())){
            return new AjaxResult(500,"用户名已存在");
        }
        user.setRole(role);
        if(!user2.getPassword().equals(user.getPassword())){
            user.setPassword(SecureUtil.md5(user.getPassword()));
        }
        userService.update(user);
        user.setUserId(user.getUserId());
        roleService.updataur(user);
        return new AjaxResult("修改成功");
    }
}
